<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
		<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
		<link rel="stylesheet" href="/css/font.css">
		<link rel="stylesheet" href="/css/xadmin.css">
	</head>

	<body>
		<div class="x-body" id="app">
			<xblock class="layui-form">
				<button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除教室</button>
				<button class="layui-btn" onclick="open_addExamRoom(this)"><i class="layui-icon"></i>添加考试教室</button>
				<a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);"
				 title="刷新"><i class="layui-icon" style="line-height:30px">ဂ</i></a>
			    <input type="hidden" name="schExamId" th:attr="value=${schExamId}"/>
			</xblock>
			<table class="layui-table">
				<thead>
					<tr>
						<th width="20">
							<div class="layui-unselect header layui-form-checkbox" lay-skin="primary"><i class="layui-icon">&#xe605;</i></div>
						</th>
						<th>考试日期</th>
						<th>考试时间</th>
						<th>考试教室</th>
						<th width="350">操作</th>
				</thead>
				<tbody>
					<tr v-for="roomName in roomNames">
						<td>
							<div class="layui-unselect layui-form-checkbox" lay-skin="primary" :data-id="roomName"><i class="layui-icon">&#xe605;</i></div>
						</td>
						<td>{{ schExam.examDate }}</td>
						<td>{{ schExam.examTime }}</td>
						<td>{{ roomName }}</td>
						<td class="td-manage">
						    <button class="layui-btn layui-btn-sm" onclick="open_editExamTeacher(this)" :data-roomName="roomName" href="javascript:;"><i
                                 class="layui-icon">&#xe613;</i>监考教师</button>
						    <button class="layui-btn layui-btn-sm" onclick="open_editExamStudent(this)" :data-roomName="roomName" href="javascript:;"><i
                                 class="layui-icon">&#xe613;</i>考试学生</button>
							<button class="layui-btn-danger layui-btn layui-btn-sm" onclick="member_del(this)" href="javascript:;"><i
								 class="layui-icon">&#xe640;</i>删除教室</button>
						</td>
					</tr>
				</tbody>
			</table>
		</div>
		<script type="text/javascript" src="/js/jquery.min.js"></script>
        <script type="text/javascript" src="/js/xadmin.js"></script>
        <script type="text/javascript" src="/lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="/js/vue.min.js"></script>
        <script type="text/javascript" src="/js/jquery.form.js"></script>
		<script>
			var vm = new Vue({
	            el : '#app',
	            data : {
	                // 考试课程信息
	                schExam : "",
	                // 考试教室
	                roomNames: "",
	                // 当前考试课程id
	                schExamId:""
	            },
	            created : function() {
	                this.schExamId = $("input[name='schExamId']").val();
	                // 获取当前考试课程信息
	               this.getSchExamWithRoom(this.schExamId);
	            },
	            methods : {
	            	getSchExamWithRoom : function(schExamId) {
	                    var vm = this;
	                    $.ajax({
	                        url : "/sys/exam/getSchExamWithRoom",
	                        data: {
	                        	schExamId: schExamId
	                        },
	                        type : "get",
	                        success : function(result) {
	                            if (result.state) {
	                                var data = result.data;
	                                vm.schExam = data.schExam;
	                                vm.roomNames = data.roomNames;
	                            } else {
	                                layui.use(['layer'], function(){
	                                    var layer = layui.layer
	                                    layer.msg(""+result.msg, {icon: 5});
	                                });
	                            }
	                        }
	                    });
	                }
	            }
	        });

			// 添加教室
			function open_addExamRoom(obj) {
				layer.open({
                    type: 1, //Page层类型
                    btn: ["确定", "取消"],
                    title: '添加考试教室',
                    skin: 'layui-layer-prompt',
                    content: "<input type='text' class='layui-layer-input' placeholder='教室名称，教学楼-教室号'>",
                    yes: function(index, layero) {
                        // 按钮【按钮一】的回调
                        var roomName = $(layero).find("input[type='text']").val();
                        if (roomName.length <= 0) {
                        	layer.msg("请输入教室名称", {
                                icon: 5
                            });
                        	return;
                        }
                        // 保存
                        $.ajax({
                            url: "/sys/exam/saveExamRoom",
                            data: {
                            	id: vm.schExamId,
                            	roomName: roomName
                            },
                            type: "post",
                            success: function(result) {
                                if (result.state) {
                                    layer.msg("添加成功", {
                                        icon: 1
                                    });
                                    vm.getSchExamWithRoom(vm.schExamId);
                                    // 关闭
                                    layer.close(index);
                                } else {
                                    layer.msg("" + result.msg, {
                                        icon: 5
                                    });
                                }
                            }
                        });
                    }
                })
			}
			
			// 打开监考教师页面
			function open_editExamTeacher(obj) {
				var roomName = $(obj).attr("data-roomName");
				x_admin_show('监考教师','/sys/exam/toEditExamTeacher?id=' + vm.schExamId 
						+ "&majorNo=" + vm.schExam.majorNo + "&roomName=" + roomName, 600, 400);
			}
			// 打开考试学生页面
            function open_editExamStudent(obj) {
            	var roomName = $(obj).attr("data-roomName");
                x_admin_show('考试学生','/sys/exam/toEditExamStudent?id=' + vm.schExamId
                		+ "&courseNo=" + vm.schExam.courseNo + "&roomName=" + roomName, 600, 400);
            }
			
			// 单个删除
			function member_del(obj) {
				var roomNames=[];
				var roomName = $(obj).parents("tr").find("div:first").attr("data-id");
				roomNames.push(roomName);
				layer.confirm('确认要删除【' + roomName + '】吗？', function(index) {
					$.ajax({
						url: "/sys/exam/delExamRoom",
						type: "post",
						data: {
							schExamId: vm.schExamId,
							roomNames: roomNames
						},
						success: function(result) {
							if (result.state) {
								layer.msg('删除成功!', {
									icon: 1,
									time: 1000
								});
								vm.getSchExamWithRoom(vm.schExamId);
							} else {
								layer.msg("" + result.msg, {
									icon: 5
								});
							}
						}
					});
				});
			}

			// 批量删除
			function delAll(argument) {
				var roomNames = tableCheck.getData();
				if (roomNames.length <= 0) {
					return;
				}
				layer.confirm('确认要删除【' + roomNames + '】吗？', function(index) {
					$.ajax({
						url: "/sys/exam/delExamRoom",
						type: "post",
						data: {
							schExamId: vm.schExamId,
							roomNames: roomNames
						},
						success: function(result) {
							if (result.state) {
								layer.msg('删除成功', {
									icon: 1
								});
								// 移除所有checkbox的勾选状态
								$(".layui-form-checkbox").removeClass('layui-form-checked');
								vm.getSchExamWithRoom(vm.schExamId);
							} else {
								layer.msg("" + result.msg, {
									icon: 5
								});
							}
						}
					});
				});
			}
			
		</script>
	</body>

</html>
